﻿// -----------------------------------------------------------------------
// <copyright file="TouchMenuRadialPanel.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TouchControls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Shapes;
    using System.Windows.Media;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    class TouchMenuRadialPanel : Panel
    {
        #region private variables
        double _Rayon=32;
        double _TotalHeight;
        double _ChildSpace;
        double _MaxWidth;
        #endregion private variables



        #region DP Properties

        public double Zoom
        {
            get { return (double)GetValue(ZoomProperty); }
            set { SetValue(ZoomProperty, value); }
        }


        public Canvas LineCanvas
        {
            get { return (Canvas)GetValue(LineCanvasProperty); }
            set { SetValue(LineCanvasProperty, value); }
        }


        public VisualState ExpandedState
        {
            get { return (VisualState)GetValue(ExpandedStateProperty); }
            set { SetValue(ExpandedStateProperty, value); }
        }

        public VisualState CollapsedState
        {
            get { return (VisualState)GetValue(CollapsedStateProperty); }
            set { SetValue(CollapsedStateProperty, value); }
        }

        public static readonly DependencyProperty LineCanvasProperty =
            DependencyProperty.Register("LineCanvas", typeof(Canvas), typeof(TouchMenuRadialPanel), null);

        public static readonly DependencyProperty ZoomProperty =
            DependencyProperty.Register("Zoom", typeof(double), typeof(TouchMenuRadialPanel), new PropertyMetadata(new PropertyChangedCallback(ZoomChanged)));
        public static void ZoomChanged(
                    DependencyObject d,
                    DependencyPropertyChangedEventArgs e
                    )
        {
            (d as TouchMenuRadialPanel).InvalidateMeasure();
        }


        public static readonly DependencyProperty ExpandedStateProperty =
            DependencyProperty.Register("ExpandedState", typeof(VisualState), typeof(TouchMenuRadialPanel), null);


        public static readonly DependencyProperty CollapsedStateProperty =
            DependencyProperty.Register("CollapsedState", typeof(VisualState), typeof(TouchMenuRadialPanel), null);


        #endregion DP Properties

        #region ctor
        public TouchMenuRadialPanel()
        {
            _ChildSpace = 5;
            Zoom = 1;

        }
        #endregion ctor

        #region panel implementation
        private Line CreateNewLine()
        {
            Line _result = new Line();
            _result.StrokeThickness = 1;
            _result.Stroke = new SolidColorBrush(Colors.Gray);
            return _result;
        }

        private void CheckLines()
        {
            if (LineCanvas != null)
            {
                int _nbLines = Children.Count;
                if (LineCanvas.Children.Count != _nbLines)
                {
                    LineCanvas.Children.Clear();
                    for (int i = 1; i <= _nbLines; i++)
                    {
                        LineCanvas.Children.Add(CreateNewLine());
                    }
                }
            }
        }




        protected override Size ArrangeOverride(Size finalSize)
        {//V5

            double _totalHeight = _TotalHeight * Zoom;
            double _centerYDelta = (finalSize.Height - _totalHeight) / 2;
            double _hSum = 0;
            double _coef = 3.9; //3.5
            if (LineCanvas != null)
                LineCanvas.Children.Clear();
            int _index = 0;
            foreach (UIElement child in Children)
            {
                double _h = (_totalHeight / 2 - _hSum);
                double XFromCenter = Math.Sqrt(_Rayon * _Rayon - _h * _h);
                double X = XFromCenter - (_Rayon - _Rayon / _coef);
                double Y = _hSum * Zoom + _centerYDelta;
                if (LineCanvas != null)
                {
                    Line _line = new Line();
                    _line.X1 = 0;
                    _line.Y1 = finalSize.Height / 2;
                    _line.X2 = X;
                    _line.Y2 = Y + child.DesiredSize.Height / 2;
                    LineCanvas.Children.Add(_line);
                }


                Rect _finalRect = new Rect(new Point(X, Y), child.DesiredSize);
                child.Arrange(_finalRect);
                _hSum += child.DesiredSize.Height + _ChildSpace;
                _index++;
            }

            return finalSize;


        }
        #endregion
    }
}
